#include "net_heart_beat.h"
#include "string.h"
#include "sys_parameter.h"
#include "order_handle.h"
#include "run.h"
#include "frequency_converter.h"
#include "usart.h"


#define heart_data_len 2000
sys_tim heart_beat_tim;
sys_tim count_down_tim;
u8 heart_data[heart_data_len]={0};

_net_heart_beat_send net_heart_beat_send;
void net_heart_beat(void)
{
	if(sys_running_timer.ms-count_down_tim.ms>=1000)
	{
		sys_status.count_down--;
		if(sys_status.count_down<0)
			sys_status.count_down=0;
		count_down_tim=sys_running_timer;
	}
	
	if(sys_running_timer.ms-heart_beat_tim.ms>=200)
	{
		switch(net_heart_beat_send)
		{
			case SEND_PRODUCT:
				memset(heart_data,0,heart_data_len);
				sprintf(heart_data,
				"{\"package_product_message\":%d,\
				\"type\":%d,\
				\"id\":%d,\
				\"version\":\"%s\",\
				\"run_mod\":%d,\
				\"count_down\":%d,\
				\"emergency_stop\":%d,\
				\"fuel_oil\":%d,\
				\"fuel_coal\":%d,\
				\"I_way\":%d,\
			  \"high_power\":%d,\
				\"unique_id\":\"%08X%08X%08X\"\
				}\r\n",
				sys_running_timer.sec,
				baseConfig.product_type,
				baseConfig.product_id,
				FIRMWARE_VERSION,
				sys_status.run_mod,
				sys_status.count_down,
				sys_status.emergency_stop,
				sys_status.fuel_oil,
				sys_status.fuel_coal,
				baseConfig.I_way,
				baseConfig.high_power,
				sys_status.ID_H,
				sys_status.ID_M,
				sys_status.ID_L
				);
				
				USART1_send_buf(heart_data, strlen(heart_data));
				net_heart_beat_send=SEND_WINDOW;
				break;
			
			case SEND_WINDOW:
				memset(heart_data,0,heart_data_len);
				sprintf(heart_data,
				"{\"package_window_s\":%d,\
				\"realy_s_1_hot_wind\":%d,\
				\"realy_s_2_hot_wind\":%d,\
				\"realy_s_3_hot_wind\":%d,\
				\"realy_s_4_hot_wind\":%d,\
				\"realy_s_1_cold_wind\":%d,\
				\"realy_s_damp_grain_transport\":%d,\
				\"realy_s_damp_grain_raise\":%d,\
				\"realy_s_initial_screening\":%d,\
				\"realy_s_before_town_raise\":%d,\
				\"realy_s_dry_grain_transport\":%d,\
				\"realy_s_dry_grain_raise\":%d,\
				\"realy_s_pull_grain\":%d,\
				\"realy_s_fire_oil\":%d,\
				\"realy_s_fire_oil_big\":%d,\
				\"realy_s_blower\":%d,\
				\"realy_s_slag_remover\":%d,\
				\"realy_s_smoking_machine\":%d,\
				\"realy_s_pull_roller\":%d,\
				\"realy_s_fire_grate\":%d,\
				\"realy_s_pull_grain_direct\":%d,\
				\"led_green\":%d,\
				\"led_yellow\":%d,\
				\"led_red\":%d,\
				\"alarm_proximity\":[%d,%d,%d,%d,%d],\
				\"alarm_temp\":[%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d],\
				\"alarm_I\":[%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d]\
				}\r\n",
				sys_running_timer.sec,
				sys_status.realy_s_1_hot_wind,
				sys_status.realy_s_2_hot_wind,
				sys_status.realy_s_3_hot_wind,
				sys_status.realy_s_4_hot_wind,
				sys_status.realy_s_1_cold_wind,
				sys_status.realy_s_damp_grain_transport,
				sys_status.realy_s_damp_grain_raise,
				sys_status.realy_s_initial_screening,
				sys_status.realy_s_before_town_raise,
				sys_status.realy_s_dry_grain_transport,
				sys_status.realy_s_dry_grain_raise,
				sys_status.realy_s_pull_grain,
				sys_status.realy_s_fire_oil,
				sys_status.realy_s_fire_oil_big,
				sys_status.realy_s_blower,
				sys_status.realy_s_slag_remover,
				sys_status.realy_s_smoking_machine,
				sys_status.realy_s_pull_roller,
				sys_status.realy_s_fire_grate,
				sys_status.realy_s_pull_grain_direct,
				sys_status.led_green,
				sys_status.led_yellow,
				sys_status.led_red,
				
				sys_status.damp_grain_raise_obstruct.alarm_s_obstruct,
				sys_status.before_town_raise_obstruct.alarm_s_obstruct,
				sys_status.pull_grain_obstruct.alarm_s_obstruct,
				sys_status.alarm_s_dryer_grain_lack,
				sys_status.alarm_s_dryer_grain_full,
				
				sys_status.high_temp_alarm_1_hot_wind,
				sys_status.high_temp_alarm_2_hot_wind,
				sys_status.high_temp_alarm_3_hot_wind,
				sys_status.high_temp_alarm_4_hot_wind,
				sys_status.high_temp_alarm_1_grain,
				sys_status.high_temp_alarm_2_grain,
				sys_status.high_temp_alarm_3_grain,
				sys_status.high_temp_alarm_4_grain,
				sys_status.high_temp_alarm_5_grain,
				sys_status.high_temp_alarm_6_grain,
				sys_status.high_temp_alarm_7_grain,
				sys_status.temp_alarm_hot_wind_mm,
				sys_status.temp_alarm_hot_wind_return,
				sys_status.low_temp_alarm_hot_wind,
				sys_status.high_temp_alarm_fire,
				sys_status.low_temp_alarm_fire,
				
				sys_status.I.i[0].alarm,
				sys_status.I.i[1].alarm,
				sys_status.I.i[2].alarm,
				sys_status.I.i[3].alarm,
				sys_status.I.i[4].alarm,
				sys_status.I.i[5].alarm,
				sys_status.I.i[6].alarm,
				sys_status.I.i[7].alarm,
				sys_status.I.i[8].alarm,
				sys_status.I.i[9].alarm,
				sys_status.I.i[10].alarm,
				sys_status.I.i[11].alarm,
				sys_status.I.i[12].alarm,
				sys_status.I.i[13].alarm,
				sys_status.I.i[14].alarm,
				sys_status.I.i[15].alarm
				);
				
				USART1_send_buf(heart_data, strlen(heart_data));
				net_heart_beat_send=SEND_TEMP;
				break;
			
			case SEND_TEMP:
				memset(heart_data,0,heart_data_len);
				sprintf(heart_data,
				"{\"package_temp\":%d,\
				\"temp_1_hot_wind\":[%d,%d],\
				\"temp_2_hot_wind\":[%d,%d],\
				\"temp_3_hot_wind\":[%d,%d],\
				\"temp_4_hot_wind\":[%d,%d],\
				\"temp_1_grain\":[%d,%d],\
				\"temp_2_grain\":[%d,%d],\
				\"temp_3_grain\":[%d,%d],\
				\"temp_4_grain\":[%d,%d],\
				\"temp_5_grain\":[%d,%d],\
				\"temp_6_grain\":[%d,%d],\
				\"temp_7_grain\":[%d,%d],\
				\"temp_fire\":[%d,%d],\
				\"humi_mid\":[%d,%d],\
				\"humi_bottom\":[%d,%d]\
				}\r\n",
				sys_running_timer.sec,
				sys_status.temp_1_hot_wind.is_online, sys_status.temp_1_hot_wind.temp_t,
				sys_status.temp_2_hot_wind.is_online, sys_status.temp_2_hot_wind.temp_t,
				sys_status.temp_3_hot_wind.is_online, sys_status.temp_3_hot_wind.temp_t,
				sys_status.temp_4_hot_wind.is_online, sys_status.temp_4_hot_wind.temp_t,
				sys_status.temp_1_grain.is_online, sys_status.temp_1_grain.temp_t,
				sys_status.temp_2_grain.is_online, sys_status.temp_2_grain.temp_t,
				sys_status.temp_3_grain.is_online, sys_status.temp_3_grain.temp_t,
				sys_status.temp_4_grain.is_online, sys_status.temp_4_grain.temp_t,
				sys_status.temp_5_grain.is_online, sys_status.temp_5_grain.temp_t,
				sys_status.temp_6_grain.is_online, sys_status.temp_6_grain.temp_t,
				sys_status.temp_7_grain.is_online, sys_status.temp_7_grain.temp_t,
				sys_status.temp_fire.is_online, sys_status.temp_fire.temp_t,
				sys_status.humi1.is_online, sys_status.humi1.humi_t,
				sys_status.humi2.is_online, sys_status.humi2.humi_t
				);
				
				USART1_send_buf(heart_data, strlen(heart_data));
				net_heart_beat_send=SEND_V_I;
				break;
			
			case SEND_V_I:
				memset(heart_data,0,heart_data_len);
				sprintf(heart_data,
				"{\"package_V_I\":%d,\
				\"ZT\":\
					{\"ZT_online\":%d,\
					\"Uab\":%d,\
					\"Ubc\":%d,\
					\"Uca\":%d,\
					\"Ua\":%d,\
					\"Ub\":%d,\
					\"Uc\":%d,\
					\"Ia\":%d,\
					\"Ib\":%d,\
					\"Ic\":%d,\
					\"P\":%d,\
					\"Q\":%d\
					},\
					\"frequency_i\":[%d,%d,%d,%d],\
					\"I\":\
					{\"I_online\":%d,\
					\"i1\":[%d,%d],\
					\"i2\":[%d,%d],\
					\"i3\":[%d,%d],\
					\"i4\":[%d,%d],\
					\"i5\":[%d,%d],\
					\"i6\":[%d,%d],\
					\"i7\":[%d,%d],\
					\"i8\":[%d,%d],\
					\"i9\":[%d,%d],\
					\"i10\":[%d,%d],\
					\"i11\":[%d,%d],\
					\"i12\":[%d,%d],\
					\"i13\":[%d,%d],\
					\"i14\":[%d,%d],\
					\"i15\":[%d,%d],\
					\"i16\":[%d,%d]\
					}\
				}\r\n",
				sys_running_timer.sec,
				sys_status.zt.is_online,
				sys_status.zt.Uab,
				sys_status.zt.Ubc,
				sys_status.zt.Uca,
				sys_status.zt.Ua,
				sys_status.zt.Ub,
				sys_status.zt.Uc,
				sys_status.zt.Ia,
				sys_status.zt.Ib,
				sys_status.zt.Ic,
				sys_status.zt.P,
				sys_status.zt.Q,
				sys_status.pull_grain.out_i, sys_status.blower.out_i, sys_status.pull_roller.out_i, sys_status.fire_grate.out_i,
				sys_status.I.is_online,
				sys_status.I.i[0].i_t, sys_status.I.i[0].alarm,
				sys_status.I.i[1].i_t, sys_status.I.i[1].alarm,
				sys_status.I.i[2].i_t, sys_status.I.i[2].alarm,
				sys_status.I.i[3].i_t, sys_status.I.i[3].alarm,
				sys_status.I.i[4].i_t, sys_status.I.i[4].alarm,
				sys_status.I.i[5].i_t, sys_status.I.i[5].alarm,
				sys_status.I.i[6].i_t, sys_status.I.i[6].alarm,
				sys_status.I.i[7].i_t, sys_status.I.i[7].alarm,
				sys_status.I.i[8].i_t, sys_status.I.i[8].alarm,
				sys_status.I.i[9].i_t, sys_status.I.i[9].alarm,
				sys_status.I.i[10].i_t, sys_status.I.i[10].alarm,
				sys_status.I.i[11].i_t, sys_status.I.i[11].alarm,
				sys_status.I.i[12].i_t, sys_status.I.i[12].alarm,
				sys_status.I.i[13].i_t, sys_status.I.i[13].alarm,
				sys_status.I.i[14].i_t, sys_status.I.i[14].alarm,
				sys_status.I.i[15].i_t, sys_status.I.i[15].alarm
				);
				
				USART1_send_buf(heart_data, strlen(heart_data));
				net_heart_beat_send=SEND_FREQ;
				break;
			
			case SEND_FREQ:
				memset(heart_data,0,heart_data_len);
				sprintf(heart_data,
				"{\"package_frequency\":%d,\
				\"freq_pull_grain\":[%d,%d,%d,%d,%d,%d,%d,%d,%d,%d],\
				\"freq_blower\":[%d,%d,%d,%d,%d,%d,%d,%d,%d,%d],\
				\"freq_pull_roller\":[%d,%d,%d,%d,%d,%d,%d,%d,%d,%d],\
				\"freq_fire_grate\":[%d,%d,%d,%d,%d,%d,%d,%d,%d,%d]\
				}\r\n",
				sys_running_timer.sec,
				
				sys_status.pull_grain.is_online,
				sys_status.pull_grain.valid,
				sys_status.pull_grain.freq,
				sys_status.pull_grain.bus_voltage,
				sys_status.pull_grain.out_voltage,
				sys_status.pull_grain.out_i,
				sys_status.pull_grain.out_p,
				sys_status.pull_grain.out_torque,
				sys_status.pull_grain.run_speed,
				freq_set_pull_grain.dest_freq,
				
				sys_status.blower.is_online, 
				sys_status.blower.valid, 
				sys_status.blower.freq,
				sys_status.blower.bus_voltage,
				sys_status.blower.out_voltage,
				sys_status.blower.out_i,
				sys_status.blower.out_p,
				sys_status.blower.out_torque,
				sys_status.blower.run_speed,
				freq_set_blower.dest_freq,
				
				sys_status.pull_roller.is_online, 
				sys_status.pull_roller.valid, 
				sys_status.pull_roller.freq,
				sys_status.pull_roller.bus_voltage,
				sys_status.pull_roller.out_voltage,
				sys_status.pull_roller.out_i,
				sys_status.pull_roller.out_p,
				sys_status.pull_roller.out_torque,
				sys_status.pull_roller.run_speed,
				freq_set_pull_roller.dest_freq,
				
				sys_status.fire_grate.is_online, 
				sys_status.fire_grate.valid, 
				sys_status.fire_grate.freq,
				sys_status.fire_grate.bus_voltage,
				sys_status.fire_grate.out_voltage,
				sys_status.fire_grate.out_i,
				sys_status.fire_grate.out_p,
				sys_status.fire_grate.out_torque,
				sys_status.fire_grate.run_speed,
				freq_set_fire_grate.dest_freq
				);
				
				USART1_send_buf(heart_data, strlen(heart_data));
				net_heart_beat_send=SEND_PRODUCT;
				break;
			
			default:
				net_heart_beat_send=SEND_PRODUCT;
				break;
		}
		heart_beat_tim=sys_running_timer;
	}
}






